#!/usr/bin/env python

"""
This file is part of open-ihm.

open-ihm is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.

open-ihm is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with open-ihm.  If not, see <http://www.gnu.org/licenses/>.
"""

__author__ = 'Sarah Mount'
__credits__ = 'Brown Msiska, Tiwonge Manda'
__date__ = 'May 2012'
__version__ = '0.1'

# pylint: disable=W0312
# pylint: disable=W0511
# pylint: disable=W0703

import os
import logging
import stat
import tempfile

from PyQt4 import QtGui, QtCore, uic

# import the main window design class
Ui_MainWindow, base_class = uic.loadUiType(os.path.dirname(__file__) + "/ui_mainwindow.ui")

#
# TODO: Add a nice function to get resource files in a platform-independent way.
#


class PicturedMDIArea(QtGui.QMdiArea):
    """This class creates an MDI area with a centred background image.

    Code is adapted from here:
    
    http://www.diotavelli.net/PyQtWiki/Adding%20a%20background%20image%20to%20an%20MDI%20area

    Thanks to David Boddie on the PyQt mailing list.
    """
    
    def __init__(self, background_pixmap, parent = None):
        QtGui.QMdiArea.__init__(self, parent)
        self.background_pixmap = background_pixmap
        self.display_pixmap = None
    
    def paintEvent(self, event):
        painter = QtGui.QPainter()
        painter.begin(self.viewport())
        painter.setBrush(QtGui.QColor(220, 220, 220))
        painter.drawRect(0, 0, self.width(), self.height())
        x = (self.width() - self.background_pixmap.width())/2
        y = (self.height() - self.background_pixmap.height())/2
        painter.drawPixmap(x, y, self.background_pixmap)
        painter.end()
    
    def resizeEvent(self, event):
        """
        Ensure that the logo stays in the centre of the screen on resize.
        """
        self.display_pixmap = self.background_pixmap.scaled(event.size(), QtCore.Qt.KeepAspectRatio)


class FrmMainWindow(QtGui.QMainWindow, Ui_MainWindow):
    """ Creates the Main Window of the application using the main 
    window design in the gui.designs.ui_mainwindow module 
    """    
    def __init__(self, parent=None):
        """Initialises Main Window. Adds the MdiArea (QMdiArea)
        """
        QtGui.QMainWindow.__init__(self)
        self.setupUi(self)

        self.projectid = -1
        self.projectname = ""

        pixmap = QtGui.QPixmap(os.path.dirname(__file__) + '/../resources/images/EfDUnimaChancoComposite.jpg')
        self.mdi = PicturedMDIArea(pixmap)
        
        self.setCentralWidget(self.mdi)

        self.actionClose_Project.setDisabled(True)

        #self.assistant = None # Help assistant

       
	### FIXME: Replace absolute paths to images with Qt resources
        self.setWindowIcon(QtGui.QIcon(os.path.dirname(__file__) + '/../resources/images/openihm.png'))

        ### FIXME: Should do this in Qt4Deisgner
        self.actionOpen_Project.setIcon(QtGui.QIcon(os.path.dirname(__file__) + '/../resources/images/projectOpen.png'))
        self.actionCreate_Project.setIcon(QtGui.QIcon(os.path.dirname(__file__) + '/../resources/images/projectNew.png'))
        self.actionFind_Project.setIcon(QtGui.QIcon(os.path.dirname(__file__) + '/../resources/images/projectFind.png'))
        self.actionClose_Project.setIcon(QtGui.QIcon(os.path.dirname(__file__) + '/../resources/images/projectClose.png'))
        
    def centerSubWindow(self, subWin):
        """ Moves a subwindow to the center of the Mdi Area
        """
        screen = self.mdi.geometry()
        size =  subWin.geometry()
        subWin.move((screen.width()-size.width())/2, (screen.height()-size.height())/2)
        
